/* GUI CONVERTED from rcsolcmt.p (converter v1.76) Wed Sep  4 05:46:15 2002 */
/* rcsolcmt.p - SALES ORDER MAINTENANCE: CONTAINER AND LINE CHARGE DATA  */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                   */
/* All rights reserved worldwide.  This is an unpublished work.          */
/* $Revision: 1.18 $                                                */
/*                                                                       */
/* Revision: 1.16        BY: Dan Herman   DATE: 07/16/01   ECO: *P006*   */
/* Revision: 1.17        BY: Dan Herman   DATE: 08/15/01   ECO: *P01L*   */
/* $Revision: 1.18 $       BY: Dan Herman       DATE: 9/03/02 ECO: *P0HG*  */
/*V8:ConvertMode=Maintenance                                             */
/*                                                                       */
/* -----  W A R N I N G  -----  W A R N I N G  -----  W A R N I N G  -----  */
/*                                                                          */
/*         THIS PROGRAM IS A BOLT-ON TO STANDARD PRODUCT MFG/PRO.           */
/* ANY CHANGES TO THIS PROGRAM MUST BE PLACED ON A SEPARATE ECO THAN        */
/* STANDARD PRODUCT CHANGES.  FAILURE TO DO THIS CAUSES THE BOLT-ON CODE TO */
/* BE COMBINED WITH STANDARD PRODUCT AND RESULTS IN PATCH DELIVERY FAILURES.*/
/*                                                                          */
/* -----  W A R N I N G  -----  W A R N I N G  -----  W A R N I N G  -----  */
/*                                                                          */
/*-------------------------------------------------------------------------
  Purpose: This program allows the user to edit and add additional
           charges to the sales order/line item.
  Notes:
----------------------------------------------------------------------------*/

{mfdeclre.i}
{pxpgmmgr.i}
{pxphdef.i cclcxr}
{gplabel.i}

define input parameter ipSoRecNo as recid no-undo.
define input parameter ipSodRecNo as recid no-undo.

define variable del-yn like mfc_logical no-undo.
define variable ctr as integer no-undo.
define variable new_sodlc_det like mfc_logical no-undo.
define variable l_is_first like mfc_logical no-undo.
define variable where_we_are as integer no-undo.
define variable sodlcdet_recid as recid no-undo.

define variable default_lc_type like ccl_charge_type no-undo.

define variable addcharge like rsn_desc
                         column-label "Additional!Charges" no-undo.
define variable vSalesAccount as character no-undo.
define variable vSubAccount as character no-undo.
define variable vCostcenter as character no-undo.
define variable vTaxable as logical no-undo.
define variable vTaxClass as character no-undo.
define variable op_price as decimal no-undo.
define variable line_price as decimal no-undo.

define  frame line_det.

/*V8:EditableDownFrame=line_det */
FORM /*GUI*/ 
   sodlc_lc_line
   addcharge view-as fill-in size 15 by 1
   sodlc_trl_code
   sodlc_lc_amt
   sodlc_one_time
   sodlc_charge_type
   sodlc_ref column-label "Ref" view-as fill-in size 15 by 1
with frame line_det down centered
scroll 1 attr-space width 80
title color normal (getFrameTitle("ADDITIONAL_LINE_CHARGES",29)) THREE-D /*GUI*/.


/* SET EXTERNAL LABELS */
setFrameLabels(frame line_det:handle).

FUNCTION code_desc returns character (input trlcode as character):
   define variable code_desc as character no-undo.
   for first trl_mstr
      fields (trl_code trl_desc)
      where
       trl_code = trlcode
       no-lock:
       code_desc = trl_desc.
   end.
   return code_desc.
END FUNCTION.


on leave of sodlc_trl_code do:
   run charge_label (input (input sodlc_trl_code),
                     output addcharge).
   display addcharge with frame line_det.

        
  do on error undo, leave:
      run q-leave in global-drop-down-utilities.
  end.
  run q-set-window-recid in global-drop-down-utilities.
  if return-value = "error" then return no-apply.   

end.


find so_mstr where recid(so_mstr) = ipSoRecNo no-lock.

find sod_det where recid(sod_det) = ipSodRecNo exclusive-lock.

/* SET SO_TAX_DATE TO A VALID*/
/* DATE IF A SCHEDULED ORDER*/
if so_sched then
   run set_tax_date in THIS-PROCEDURE
      (input so_nbr,
       input today).

for first ccl_ctrl
   fields (ccl_charge_type)
   no-lock:
   default_lc_type = ccl_charge_type.
end.

for first ccls_mstr
   fields(ccls_shipfrom ccls_shipto
          ccls_charge_type ccls_edit_lc_shipper)
   no-lock where
       ccls_shipfrom = sod_site
   and ccls_shipto = (if sod_dock <> "" then sod_dock
                      else if sod_ship <> "" then sod_ship
          else so_ship):
   default_lc_type = ccls_charge_type.
end.
if not available ccls_mstr then
   for first ccls_mstr
      fields(ccls_shipfrom ccls_shipto
             ccls_charge_type ccls_edit_lc_shipper)
      no-lock where
         ccls_shipfrom = ""
      and ccls_shipto = (if sod_dock <> "" then sod_dock
                        else if sod_ship <> "" then sod_ship
                        else so_ship):
      default_lc_type = ccls_charge_type.
   end.

view frame line_det.

lineloop:
do with frame line_det:
   clear frame line_det all no-pause.
   view frame line_det.

   for first sodlc_det no-lock
      where
         sodlc_order    = sod_nbr
      and sodlc_ord_line = sod_line:
   end.
   ctr = 0.
   do while ctr < frame-down and available sodlc_det:
      addcharge = code_desc(sodlc_trl_code).

      display
         sodlc_lc_line
         addcharge
         sodlc_trl_code
         sodlc_lc_amt
         sodlc_one_time
         sodlc_charge_type
         sodlc_ref.

         ctr = ctr + 1.
         if ctr < frame-down then down 1.

         find next sodlc_det no-lock
            where
               sodlc_order    = sod_nbr
            and sodlc_ord_line = sod_line
         no-error.
   end. /*DO WHILE CTR <*/

   up frame-line - 1.

   for first sodlc_det no-lock
      where
         sodlc_order    = sod_nbr
      and sodlc_ord_line = sod_line:
   end.

   priceloop:
   repeat with frame line_det:
/*GUI*/ if global-beam-me-up then undo, leave.

      ststatus = stline[1].
      status input ststatus.

      pause before-hide.
      prompt-for sodlc_lc_line
         go-on(F9 F10 CURSOR-UP CURSOR-DOWN).
      pause 0.

      if frame-field = "sodlc_lc_line" then
                        next-prompt sodlc_lc_line.

      do while
         lastkey = keycode("F9")
         or lastkey = keycode("CURSOR-UP")
         or lastkey = keycode("F10")
         or lastkey = keycode("CURSOR-DOWN")
         on endkey undo, leave priceloop:

         if available sodlc_det then do:
            if lastkey = keycode("F9")
               or lastkey = keycode("CURSOR-UP")
               then do:

               find prev sodlc_det
                  where
                     sodlc_order = sod_nbr
                  and sodlc_ord_line = sod_line
               exclusive-lock no-error.

               if available sodlc_det then do:
                  up 1.
                  addcharge = code_desc(sodlc_trl_code).

                  display
                     sodlc_lc_line
                     addcharge
                     sodlc_trl_code
                     sodlc_lc_amt
                     sodlc_one_time
                     sodlc_charge_type
                     sodlc_ref.
               end.
               else do:
                  for first sodlc_det
                     where
                        sodlc_order = sod_nbr
                     and sodlc_ord_line = sod_line
                     exclusive-lock:
                  end.
                  bell.
               end.
            end. /* if F9 or CURSOR-UP */

            else
               if lastkey = keycode("F10")
               or lastkey = keycode("CURSOR-DOWN")
               then do:

               find next sodlc_det
                  where
                     sodlc_order = sod_nbr
                  and sodlc_ord_line = sod_line
               exclusive-lock no-error.

               if available sodlc_det then do:
                  down 1.
                  addcharge = code_desc(sodlc_trl_code).

                  display
                     sodlc_lc_line
                     addcharge
                     sodlc_trl_code
                     sodlc_lc_amt
                     sodlc_one_time
                     sodlc_charge_type
                     sodlc_ref.
               end.
               else do:
                  for last sodlc_det
                     where
                        sodlc_order = sod_nbr
                     and sodlc_ord_line = sod_line
                     exclusive-lock:
                  end.
                  bell.
               end.
            end. /* if F10 or CURSOR-DOWN */
         end.
/*GUI*/ if global-beam-me-up then undo, leave.
 /* if available sodlc_det */

         pause before-hide.
         prompt-for sodlc_lc_line
            go-on(F9 F10 CURSOR-UP CURSOR-DOWN).
         pause 0.

         if frame-field = "sodlc_lc_line" then
                        next-prompt sodlc_lc_line.

      end. /* do while lastkey F9 */

      for first sodlc_det
         where
            sodlc_order     = sod_nbr
         and sodlc_ord_line  = sod_line
         and sodlc_lc_line   = input sodlc_lc_line
         exclusive-lock:
      end.

      if input sodlc_lc_line < 1 or
         input sodlc_lc_line = "" then
         next priceloop.

      if not available sodlc_det then do:

         new_sodlc_det = yes.
         {pxmsg.i &MSGNUM=1 &ERRORLEVEL=1}
         /* ADDING NEW RECORD */

         create sodlc_det.
         assign
            sodlc_order      = sod_nbr
            sodlc_ord_line   = sod_line
            sodlc_lc_line    = input sodlc_lc_line
            sodlc_charge_type = default_lc_type
            sodlc_mod_userid = global_userid
            sodlc_mod_date   = today.

         if recid(sodlc_det) = -1 then .
      end.

      else new_sodlc_det = no.

      assign
         sodlcdet_recid = recid(sodlc_det)
         where_we_are = frame-line.

      /* SET RECORD POINTER BEFORE ITERATIVELY FINDING PREV RECORD. */
      for first sodlc_det
         where recid(sodlc_det) = sodlcdet_recid
         exclusive-lock:
      end.

      if new_sodlc_det and frame-line <> frame-down
         then do:
         do ctr = 1 to frame-down - 1  while available sodlc_det:
            find prev sodlc_det
               where
                  sodlc_order = sod_nbr
               and sodlc_ord_line = sod_line
            no-lock no-error.
         end. /* DO I = 1 TO FRAME-DOWN - 1 */

         if available sodlc_det
         then do:
            find prev sodlc_det
               where
                  sodlc_order    = sod_nbr
               and sodlc_ord_line = sod_line
            no-lock no-error.
         end. /* IF AVAIL SODLC_DET  */
      end. /* IF NEW_SODLC_DET AND.. */

      else
         do ctr = 1 to where_we_are while available sodlc_det:
            find prev sodlc_det
               where
                  sodlc_order = sod_nbr
               and sodlc_ord_line = sod_line
            no-lock no-error.
      end.

      /* DETERMINE IF THE TOP MOST RECORD IN THE FRAME IS VERY FIRST */
      /* sodlc_det RECORD */
      if not available sodlc_det
      then
         l_is_first = yes.
      else
         l_is_first = no.

      up where_we_are - 1.

      do ctr = 1 to frame-down:
         if ctr = 1 and l_is_first
         then
            for first sodlc_det
               where
                  sodlc_order  = sod_nbr
               and sodlc_ord_line = sod_line
            no-lock:
         end.
         else
            find next sodlc_det
               where
                  sodlc_order = sod_nbr
               and sodlc_ord_line = sod_line
            no-lock no-error.

         if available sodlc_det then do:
            addcharge = code_desc(sodlc_trl_code).

            display
               sodlc_lc_line
               addcharge
               sodlc_trl_code
               sodlc_lc_amt
               sodlc_one_time
               sodlc_charge_type
               sodlc_ref.

            if recid(sodlc_det) = sodlcdet_recid then do:
               where_we_are = frame-line.
            end.
         end.
         else do:
            display
               "" @ sodlc_lc_line
               "" @ addcharge
               "" @ sodlc_trl_code
               "" @ sodlc_lc_amt
               "" @ sodlc_one_time
               "" @ sodlc_charge_type
               "" @ sodlc_ref.
         end.

         if ctr < frame-down then down 1.
      end. /* do ctr = 1 to frame-down */

      up frame-down - where_we_are.

      for first sodlc_det
         where
            recid(sodlc_det) = sodlcdet_recid
         exclusive-lock:
      end.

      sodlcdet_recid = recid(sodlc_det).
      ststatus = stline[2].
      status input ststatus.

      codeloop:
      do on error undo, retry:
/*GUI*/ if global-beam-me-up then undo, leave.

         pause before-hide.

         update
            sodlc_trl_code when( sodlc_owned_by = "")
            sodlc_lc_amt when( sodlc_owned_by = "")
            sodlc_one_time when( sodlc_owned_by = "")
            sodlc_charge_type when( sodlc_owned_by = "")
            sodlc_ref
            go-on(F5 CTRL-D )
         with frame line_det editblk: editing:

            if frame-field = "sodlc_trl_code"
            then do:
               {mfnp05.i trl_mstr
                         trl_code
                         true
                         trl_code
                         sodlc_trl_code
                         ststatus}
               if recno <> ? then do:
                  display
                     trl_code @ sodlc_trl_code
                     trl_desc @ addcharge
                  with frame line_det.
               end.
            end.
            else if frame-field = "sodlc_charge_type"
               then do:
               {mfnp09.i cct_mstr
                         sodlc_charge_type
                         cct_charge_type
                         cct_container_type
                         false
                         cct_charge_type}
               if recno <> ? then do:
                  display
                     cct_charge_type @ sodlc_charge_type
                     with frame line_det.
               end.
            end. /*ELSE IF*/
            else do:
               readkey.
               apply lastkey.
            end.

            if go-pending then do:
               if input sodlc_trl_code = ""
                  then do:
                     /* BLANK NOT ALLOWED*/
                  {pxmsg.i &MSGNUM=40 &ERRORLEVEL=3}
                  next-prompt sodlc_trl_code
                  with frame line_det.
                  next editblk.
               end.

               if not can-find(first trl_mstr where
                  trl_code = input sodlc_trl_code)
                  then do:
                  /*TRAILER CODE NOT DEFINED*/
                  {pxmsg.i &MSGNUM=4446 &ERRORLEVEL=3}
                  next-prompt sodlc_trl_code
                  with frame line_det.
                  next editblk.
               end.
               if can-find(first sodlc_det where
                  sodlc_order = sod_nbr
                  and sodlc_ord_line = sod_line
                  and sodlc_trl_code = input sodlc_trl_code
                  and sodlc_lc_line <> input sodlc_lc_line
                  and sodlc_owned_by = "")
                  then do:
                  /*CODE ALREADY USED...*/
                  {pxmsg.i &MSGNUM=4448 &ERRORLEVEL=3}
                  next-prompt sodlc_trl_code.
                  next editblk.
               end. /* IF CAN-FIND(SODLC_DET...*/
               if input sodlc_charge_type <> "" and
                  not can-find(first cct_mstr where
                               cct_charge_type = input sodlc_charge_type
                               and cct_container_type = no)
                  then do:
                  {pxmsg.i &MSGNUM=4396 &ERRORLEVEL=3}
                  /*NOT A VALID VHARGE TYPE*/
                  next-prompt sodlc_charge_type
                  with frame line_det.
                  next editblk.
               end.
            end. /*if go-pending*/
         end. /*editing*/
      end.
/*GUI*/ if global-beam-me-up then undo, leave.
 /*codeloop*/
      if lastkey = keycode("F5")
         or lastkey = keycode("CTRL-D") then do:
         del-yn = yes.
         {pxmsg.i &MSGNUM=11 &ERRORLEVEL=1 &CONFIRM=del-yn}
         /* PLEASE CONFIRM DELETE */
         if del-yn = no then do:
            addcharge = code_desc(sodlc_trl_code).

            display
               sodlc_lc_line
               addcharge
               sodlc_trl_code
               sodlc_one_time
               sodlc_lc_amt
               sodlc_charge_type
               sodlc_ref.

            next priceloop.
         end.


         for first sodlc_det
            where
               sodlc_order = sod_nbr
            and sodlc_ord_line = sod_line
            and sodlc_lc_line = input sodlc_lc_line
            exclusive-lock:
         end.

         if sodlc_owned_by > "" or
            can-find(first absl_det where
               absl_order = sod_nbr
               and absl_ord_line = sod_line
               and absl_lc_line = input sodlc_lc_line)
               then do:
               {pxmsg.i &MSGNUM=836 &ERRORLEVEL=3}
               /*CAN NOT DELETE LINES WITH ACTIVITY RECORDED*/
               next priceloop.
         end.

         delete sodlc_det.

         where_we_are = frame-line.

         find next sodlc_det
            where
               sodlc_order = sod_nbr
            and sodlc_ord_line = sod_line
         no-lock no-error.

         if available sodlc_det then do:
            assign
               sodlcdet_recid = recid(sodlc_det)
               addcharge = code_desc(sodlc_trl_code).

            display
               sodlc_lc_line
               addcharge
               sodlc_trl_code
               sodlc_lc_amt
               sodlc_one_time
               sodlc_charge_type
               sodlc_ref.

            do ctr = where_we_are to frame-down - 1:
               down 1.
               find next sodlc_det
                  where
                     sodlc_order = sod_nbr
                  and sodlc_ord_line = sod_line
               no-lock no-error.
               if available sodlc_det then do:
                  addcharge = code_desc(sodlc_trl_code).

                  display
                     sodlc_lc_line
                     addcharge
                     sodlc_trl_code
                     sodlc_lc_amt
                     sodlc_one_time
                     sodlc_charge_type
                     sodlc_ref.
               end.

               else do:

                  display
                     "" @ sodlc_lc_line
                     "" @ addcharge
                     "" @ sodlc_trl_code
                     "" @ sodlc_lc_amt
                     "" @ sodlc_one_time
                     "" @ sodlc_charge_type
                     "" @ sodlc_ref.
               end.
            end. /* do ctr = where_we_are */

            up frame-down - where_we_are.
            for first sodlc_det
               where
                  recid(sodlc_det) = sodlcdet_recid
               exclusive-lock:
            end.

         end. /* if available sodlc_det */

         else do:
            display
               "" @ sodlc_lc_line
               "" @ addcharge
               "" @ sodlc_trl_code
               "" @ sodlc_one_time
               "" @ sodlc_lc_amt
               "" @ sodlc_charge_type
               "" @ sodlc_ref.

            find prev sodlc_det
               where
                  sodlc_order = sod_nbr
               and sodlc_ord_line = sod_line
            exclusive-lock no-error.

            if available sodlc_det then do:
               up 1.
               addcharge = code_desc(sodlc_trl_code).

               display
                  sodlc_lc_line
                  addcharge
                  sodlc_trl_code
                  sodlc_lc_amt
                  sodlc_one_time
                  sodlc_charge_type
                  sodlc_ref.
            end.
         end.
      end. /* if lastkey = F5 */

      else if keyfunction(lastkey) = "END-ERROR":U
         then do:
         addcharge = code_desc(sodlc_trl_code).

         display
            addcharge
            sodlc_trl_code
            sodlc_lc_amt
            sodlc_one_time
            sodlc_charge_type
            sodlc_ref.
      end.

      else do:

         assign
            line_price = if sod_qty_ord < 0 then
                         (input sodlc_lc_amt * -1) else input sodlc_lc_amt
            sodlc_trl_code = input sodlc_trl_code
            sodlc_one_time
            sodlc_charge_type = input sodlc_charge_type
            sodlc_ref = input sodlc_ref.

         {pxrun.i &PROC  = 'getLineChargeAccounts'
                  &PROGRAM = 'cclcxr.p'
                  &HANDLE = ph_cclcxr
                  &PARAM = "(input sodlc_trl_code,
                             output vSalesAccount,
                             output vSubAccount,
                             output vCostcenter,
                             output vTaxable,
                             output vTaxClass)"}


         {pxrun.i &PROC  = 'updateChargeTypePrice'
                  &PROGRAM = 'cclcxr.p'
                  &HANDLE = ph_cclcxr
                  &PARAM = "(input sodlc_charge_type,
                             input no,
                             input-output line_price,
                             input ipSoRecNo,
                             input ipSodRecNo,
                             input recid(sodlc_det),
                             input ?,
                             input ?,
                             output op_price)"}

         assign
            sodlc_acct = vSalesAccount
            sodlc_sub = vSubAccount
            sodlc_cc = vCostCenter
            sodlc_taxable = vTaxable
            sodlc_taxc = vTaxClass
            sodlc_ext_price = op_price
            sodlc_lc_amt = if sod_qty_ord < 0 then (line_price * -1)
                             else line_price.

         display sodlc_lc_amt with frame line_det.

         if new_sodlc_det and frame-line <> frame-down then do:
            assign
               sodlcdet_recid = recid(sodlc_det)
               where_we_are = frame-line.

            do ctr = 1 to where_we_are + 1
               while available sodlc_det:

               find prev sodlc_det
                  where
                     sodlc_order = sod_nbr
                  and sodlc_ord_line = sod_line
               no-lock no-error.
            end.

            up where_we_are - 1.

            /* DETERMINE IF THE TOP MOST RECORD IN THE FRAME */
            /* IS VERY FIRST SODLC_DET RECORD                */

            if not available sodlc_det
               then l_is_first = yes.
            else l_is_first = no.

            do ctr = 1 to frame-down:
               if ctr = 1 and l_is_first
                  then
                  for first sodlc_det
                     where
                        sodlc_order = sod_nbr
                     and sodlc_ord_line = sod_line
                     no-lock :
               end. /* FOR FIRST SODLC_DET */

               else
                  find next sodlc_det
                     where
                        sodlc_order = sod_nbr
                     and sodlc_ord_line = sod_line
               no-lock no-error.

               if available sodlc_det then do:
                  addcharge = code_desc(sodlc_trl_code).

                  display
                     sodlc_lc_line
                     addcharge
                     sodlc_trl_code
                     sodlc_lc_amt
                     sodlc_one_time
                     sodlc_charge_type
                     sodlc_ref.

                  if recid(sodlc_det) = sodlcdet_recid then do:
                     where_we_are = frame-line.
                  end.
               end.

               else do:
                  display
                     "" @ sodlc_lc_line
                     "" @ addcharge
                     "" @ sodlc_trl_code
                     "" @ sodlc_lc_amt
                     "" @ sodlc_one_time
                     "" @ sodlc_charge_type
                     "" @ sodlc_ref.
               end.

               if ctr < frame-down then down 1.

            end. /* do ctr = 1 to frame-down */

            up frame-down - where_we_are.
            for first sodlc_det
               where
               recid(sodlc_det) = sodlcdet_recid
               exclusive-lock:
            end.

            sodlcdet_recid = recid(sodlc_det).
         end. /* If new_sodlc_det */
      end. /* Else do */
   end. /*Priceloop*/
end. /*LINELOOP*/
hide frame line_det no-pause.


PROCEDURE charge_label:
   define input parameter chargecode like trl_code no-undo.
   define output parameter chargedesc like trl_desc no-undo.

   for first trl_mstr
      fields (trl_code trl_desc)
      where
       trl_code = chargecode
       no-lock:
       chargedesc = trl_desc.
   end.

END PROCEDURE.

PROCEDURE set_tax_date:
   define input parameter ip_so_nbr like so_nbr no-undo.
   define input parameter ip_tax_date like so_tax_date no-undo.

   define buffer b_so_mstr for so_mstr.

   for first b_so_mstr where b_so_mstr.so_nbr = ip_so_nbr:
      if b_so_mstr.so_tax_date = ? then
         b_so_mstr.so_tax_date = ip_tax_date.
   end.

END PROCEDURE.
